package com.shangcheng.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.common.Result;
import com.shangcheng.entity.TCategory;
import com.shangcheng.entity.TProduct;
import com.shangcheng.service.CategoryServiceImpl;
import com.shangcheng.service.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/public")
public class ProductController {
    @Autowired
    private CategoryServiceImpl cateservice;
    @Autowired
    private ProductServiceImpl productservice;

    // 1.商品分类列表
    @GetMapping("/categorys")
    public Result<List<TCategory>> getCategories() {
        return Result.ok(cateservice.selectAll());
    }
//    @GetMapping("/categorys/page")
//    public Page<TCategory> getCateByPage(
//            @RequestParam(defaultValue = "1") int page,
//            @RequestParam(defaultValue = "2") int size){
//        return cateservice.pageCategory(page,size);
//    }
//    @GetMapping("/categorys/{categoryType}")
//    public Page<TCategory> getCateByTypePage(
//            @PathVariable int type,
//            @RequestParam(defaultValue = "1") int page,
//            @RequestParam(defaultValue = "2") int size) {
//        return cateservice.pageCategoryByType(type, page, size);
//    }
//    @GetMapping("/products/page")
//    public Page<TProduct> getProductsByPage(
//            @RequestParam(defaultValue = "1") int page,
//            @RequestParam(defaultValue = "2") int size) {
//        return productservice.pageProducts(page, size);
//    }

//    // 2.保存商品信息
//    @PostMapping("/products")
//    public Result<Integer> addProduct(@RequestBody TProduct product) {
//        productservice.addProduct(product);
//        return Result.ok(product.getId());
//    }

    // 3.获取所有商品（支持按分类筛选）
//    @GetMapping("/products")
//    public Result<List<TProduct>> findProducts(@RequestParam(required = false) Integer categoryId) {
//        List<TProduct> products = categoryId != null
//                ? productservice.getProductsByCategory(categoryId)
//                : productservice.allProducts();
//        return Result.ok(products);
//    }
//分页
@GetMapping("/products")
public Result<Page<TProduct>> findProducts(
        @RequestParam(required = false) Integer categoryId,
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int size) {

    Page<TProduct> productPage = categoryId != null
            ? productservice.pageProductsByCategory(categoryId, page, size)
            : productservice.pageProducts(page, size);

    return Result.ok(productPage);
}
    // 4.新增：搜索商品（支持按分类筛选）
    @GetMapping("/products/search")
    public Result<List<TProduct>> searchProducts(
            @RequestParam("query") String query,
            @RequestParam(required = false) Integer categoryId) {

        List<TProduct> searchResults = categoryId != null
                ? productservice.searchProductsByCategory(query, categoryId)
                : productservice.searchProducts(query);

        return Result.ok(searchResults);
    }

    // 根据商品id获取商品详细信息
    @GetMapping("/products/{id}")
    public Result<TProduct> getProductById(@PathVariable Integer id) {
        TProduct product = productservice.getProductById(id);
        return Result.ok(product);
    }
    @DeleteMapping("/deleteorders/{productId}")
    public Result<Void> deleteProduct(@PathVariable("productId") Long productId) {
        productservice.deletProduct(productId);
        return Result.ok(null);
    }
}